package com.javaspeak.designpatterns.go4.creational.prototype;

/**
 * The prototype factory pattern uses a map of pre-initialized instances where
 * there is one of each type in the map.
 * <p>
 * When an instance of a certain type is requested the relevant instance
 * is retrieved and cloned.
 * <p>
 * This pattern is typically used where expensive initialisation in the
 * constructor is present so it is cheaper to clone the class than do an
 * expensive initialisation every time a new instance is required.
 * <p>
 * In this example there is a map of different shapes in the PrototypeFactory
 * <p>
 * When a Shape such as a Square is requested using the getShape() method, the
 * appropriate instance is retrieved from the map and cloned.
 *
 * @author John Dickerson
 */
public class ApplicationPrototype {

    /**
     * Retrieves a cloned copy of the required Shape from the Prototype Factory
     *
     * @throws CloneNotSupportedException
     */
    public void draw() throws CloneNotSupportedException {

        ProtypeFactory.getShape( ShapeType.SQUARE ).draw();
        ProtypeFactory.getShape( ShapeType.TRIANGLE ).draw();
    }

    /**
     * Main Method
     *
     * @param args
     */
    public static void main(String[] args) throws CloneNotSupportedException {

        ApplicationPrototype application = new ApplicationPrototype();
        application.draw();
    }
}
